package org.dllwh.alibaba.nacos.plugin.datasource.impl.base;

import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoTagMapperByMySql;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import org.dllwh.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
import org.dllwh.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;

import java.util.Collections;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName BaseConfigInfoTagMapper
 * @Description ConfigTagsRelationMapper的基本实现。
 * @Date 2024-11-23 12:26
 * @版本 V 1.0.1
 * @since JDK 17
 */
public abstract class BaseConfigInfoTagMapper extends ConfigInfoTagMapperByMySql {

    private DatabaseDialect databaseDialect;

    public BaseConfigInfoTagMapper() {
        databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
    }

    @Override
    public String getTableName() {
        return TableConstant.CONFIG_INFO_TAG;
    }

    @Override
    public MapperResult findAllConfigInfoTagForDumpAllFetchRows(MapperContext context) {
        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();
        String innerSql = databaseDialect
                .getLimitPageSqlWithOffset("SELECT id FROM config_info_tag  ORDER BY id ", startRow, pageSize);
        String sql = " SELECT t.id,data_id,group_id,tenant_id,tag_id,app_name,content,md5,gmt_modified " + " FROM (  "
                + innerSql + "  ) " + "g, config_info_tag t  WHERE g.id = t.id  ";
        return new MapperResult(sql, Collections.emptyList());
    }

    public abstract String getDataSource();

    public abstract String[] getPrimaryKeyGeneratedKeys();
}
